const app = getApp();
var config = app.globalData.config;
const http = require('../../utils/http.js');

Page({
	data: {
		picsMax: 9,
		pics: [], //上传的图片
		detailPics: [], //上传的结果图片集合
		dcontent:""	,//描述
		curriculumList:[],
		curriculum_index:null,
		curriculum_id:0
	},
	onReady() {
		if (!app.login()) {
			return;
		}
	},
	onLoad(options) {
		this.getTeacherCurriculumList();
	},
	getTeacherCurriculumList() { //获取教师管理课程
		var that = this;
		http.httpPost(config.getTeacherCurriculumList, {
		}, function(e) {
			if (e.code == 1) {
				// console.log(e);
				that.setData({
					'curriculumList': e.data
				});
			} else {
		
			}
		
		});
	},
	uploadfile() {
		let that = this
		//选取图片
		wx.chooseImage({
			count: 3,
			sizeType: ['original'], //原图
			sourceType: ['album', 'camera'], //支持选取图片
			success(res) {
				// tempFilePath可以作为img标签的src属性显示图片
				const tempFilePaths = res.tempFilePaths[0];
				for (const key in res.tempFilePaths) {
					if (Object.hasOwnProperty.call(res.tempFilePaths, key)) {
						const element = res.tempFilePaths[key];
						that.uploadfilepath(element)
					}
				}
			}
		})
	},
	getDcontent:function(e){	//获取内容
		let dcontent = e.detail.value;
		this.setData({
			dcontent: dcontent
		});
	},
	uploadfilepath(tempFilePaths) {
		var that = this;
		wx.showLoading({
			title: '上传图片中...',
			mask: true,
		})
		//上传图片
		wx.uploadFile({
			//请求后台的网址
			url: config.imgUpload,
			//小程序本地的路径
			filePath: tempFilePaths,
			//后台获取我们图片的key
			name: 'file',
			//额外的参数formData
			formData: {
				'from': 'xcx'
			},
			header: {
				token: app.globalData.token
			},
			success: function(res) {
				//上传成功
				// console.log(res, 'success');
				var _data = JSON.parse(res.data);
				// console.log(_data);
				that.data.pics.push(_data.data.fullurl);
				that.data.detailPics.push(_data.data.url);
				that.setData({
					pics: that.data.pics,
					detailPics:that.data.detailPics
				})
				// console.log(that.data.pics);
				wx.hideLoading();
			},
			fail: function(res) {
				console.log(res, 'fail');
			},
		})
	},
	pushBtn(){
		var that = this;
		var dcontent = this.data.dcontent;
		var _images = this.data.detailPics;
		var _curriculum_id = this.data.curriculum_id;
		
		if(_curriculum_id==0){
			wx.showToast({
				title: '请选择要发布的课程',
				icon: 'none',
				duration: 2000,
				mask: true
			});
			return false;
		}
		
		if(dcontent=="" || dcontent==undefined){
			wx.showToast({
				title: '请输入【分享内容】',
				icon: 'none',
				duration: 2000,
				mask: true
			});
			return false;
		}
		
		if (_images){_images = _images.toString()}
		
		http.httpPost(config.pushDynamic, {
			dcontent:dcontent,
			images:_images,
			curriculum_id:_curriculum_id
		}, function(e) {
			var _code = e.code;
			var _msg = e.msg;
			var _icon = "none";
			var _duration = 2000;
			if(_code==1){
				_icon = "success";
			}else{
				_icon = "error";
				_duration = 3500;
			}
			
			wx.showToast({
				title: _msg,
				icon: _icon,
				duration: _duration,
				mask: true
			})
			
			if(_code==1){
				setTimeout(function(){
					wx.redirectTo({
					  url: '../workbench/workbench'
					})
				},2000);
			}
			
		});
		
	},
	//发布课程
	curriculumChange: function(e) {
		let curriculum_index = e.detail.value; //选中的课程列表下标
		let curriculum_id = this.data.curriculumList[curriculum_index].id;
		this.setData({
			curriculum_index: curriculum_index,
			curriculum_id:curriculum_id
		});
		
	},
	selectimages() { //图片上传
		var that = this;
		var detailPics = that.data.pics;
		var maxCount = that.data.picsMax;
		if (detailPics.length >= maxCount) {
			wx.showToast({
				title: '最多选择' + maxCount + '张！',
			})
			return;
		}
		wx.chooseImage({
			count: 1, // 最多可以选择的图片张数，默认9
			sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
			sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
			success: function(res) {
				var imgs = res.tempFilePaths;
				// for (var i = 0; i < imgs.length; i++) {
				// 	that.data.pics.push(imgs[i])
				// }
				// that.setData({
				// 	pics: that.data.pics
				// })
				// console.log(imgs)
				that.uploadimg(imgs);
			},
		})
	},
	//多张图片上传
	uploadimg: function(fdata) {
		let that = this;
		wx.showLoading({
			title: '上传图片中...',
			mask: true,
		})
		wx.uploadFile({
			url: config.imgUpload,
			filePath: fdata[0],
			name: 'image',
			formData: {
				type: 'goods'
			},
			success(res) {
				console.log(res)
				console.log(JSON.parse(res.data).view_url);

				that.data.pics.push(JSON.parse(res.data).view_url)
				that.setData({
					pics: that.data.pics
				})
				console.log(that.data.pics);
				wx.hideLoading();
			},
			fail: () => {
				this.setData({
					isSubmit: true
				})
			}
		})
	},
	onShow() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})
